<template>
  <!-- 页面主体区域 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="home-logo">
        <img src="@/assets/heima.png" alt />
        <h1>电商后端管理平台</h1>
      </div>
      <el-button type="info" @click="handleLogout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px': '200px'">
        <div class="aside-collapse" :class="{'rotate45': isCollapse}" @click="handleToggleCollapse"><span>|||</span></div>
        <!-- 侧边菜单导航 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activeNavPath"
        >
          <!-- 一级菜单区 -->
          <el-submenu :index="item.id + ''" v-for="(item,index) in menuslist" :key="item.id">
            <template slot="title">
              <i :class="icons[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="handleAsideItemCheck('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    // 1. 获取左侧菜单
    this.getAsideMenu()

    // 获取seesionStoage高亮的路径
    this.activeNavPath = sessionStorage.getItem('activeNavPath')
  },
  data() {
    return {
      menuslist: [], // 菜单数据
      icons: [
        'iconfont icon-user',
        'iconfont icon-tijikongjian',
        'iconfont icon-shangpin',
        'iconfont icon-danju',
        'iconfont icon-baobiao'
      ],
      isCollapse: false,
      // 高亮的左侧项, 将保存下sessionStorage中
      activeNavPath: ''
    }
  },
  methods: {
    // ---------------------- 事件监听  ---------------------------
    handleLogout() {
      // 实现退出功能原理
      //  1. 通过window.seesionStorage.removeItem(key) 清除token值
      //  2. 通过链式导航跳转到登录页面
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 实现左边菜单的折叠和展开
    handleToggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击将 选项的 index保存起来, 让其能高亮
    handleAsideItemCheck(path) {
      // 将该路径保存在seesionStorage中, 刷新时页面选中
      this.activeNavPath = path
      window.sessionStorage.setItem('activeNavPath', path)
    },
    // --------------------- 发送网络去请求 -----------------------
    async getAsideMenu() {
      const { data: res } = await this.$http.get('menus')

      if (res.meta.status !== 200) return this.$showToast.error(res.meta.msg)
      this.menuslist = res.data
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  background: #373d41;
  .home-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      margin-right: 20px;
    }
  }
}

.el-aside {
  background: #333744;
  .el-menu {
    border-right: 0;
  }
  .aside-collapse {
    color: #ffffff;
    text-align: center;
    padding: 5px 0;
    background-color: #4a5064;
    cursor: pointer;
    font-size: 10px;
    letter-spacing: 0.4em;
  }
}

.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}

.rotate45 span {
  display: inline-block;
  transform: rotate(90deg);
  letter-spacing: 0.1em;
}

</style>
